package com.xiaoma.thread.wait.a1b2c3d4;

/**
 * @author maj
 * @ClassName Thread2
 * @Description
 * @date 2020-09-29 9:12
 */
public class Thread2 extends Thread {
  //定义锁对象，负责接收传入对象
  private  final Object lock;
  //定义共有构造方法
  public Thread2(Object lock) {
    this.lock = lock;
  }

  @Override
  public void run() {
    String[] string = {"1", "2", "3", "4"};
    for (int i = 0; i < string.length; i++) {
      synchronized (lock) {
        //线程2开始执行
        lock.notify();
        try {
          //线程2开始等待
          lock.wait();
        } catch (InterruptedException e) {

          e.printStackTrace();
        }
        //线程2继续执行
        System.out.print(string[i]);
        //线程2运行结束
        lock.notify();
      }
    }
  }
}

